<?php

class Configuration {

    private $_offline;
    private $_offlineMessage;
    private $_siteName;
    private $_linePerPage;
    private $_keyword;
    private $_address;
    private $_email;
    private $_marquee;
    private $_phoneNumber;
    private $_sales;

    public function getSales() {
        return $this->_sales;
    }

    public function setSales($sales) {
        $this->_sales = $sales;
    }

    public function getPhoneNumber() {
        return $this->_phoneNumber;
    }

    public function setPhoneNumber($phoneNumber) {
        $this->_phoneNumber = $phoneNumber;
    }

    public function setOffline($offline) {
        $this->_offline = $offline;
    }

    public function getOffline() {
        return $this->_offline;
    }

    public function setOfflineMessage($offlineMessage) {
        $this->_offlineMessage = $offlineMessage;
    }

    public function getOfflineMessage() {
        return $this->_offlineMessage;
    }

    public function setSiteName($siteName) {
        $this->_siteName = $siteName;
    }

    public function getSiteName() {
        return $this->_siteName;
    }

    public function setLinePerPage($linePerPage) {
        $this->_linePerPage = $linePerPage;
    }

    public function getLinePerPage() {
        return $this->_linePerPage;
    }

    public function setKeyWord($keyWord) {
        $this->_keyword = $keyWord;
    }

    public function getKeyWord() {
        return $this->_keyword;
    }

    public function setAddress($address) {
        $this->_address = $address;
    }

    public function getAddress() {
        return $this->_address;
    }

    public function setEmail($email) {
        $this->_email = $email;
    }

    public function getEmail() {
        return $this->_email;
    }

    public function setMarquee($marquee) {
        $this->_marquee = $marquee;
    }

    public function getMarquee() {
        return $this->_marquee;
    }

    // lấy thông tin cấu hình
    public function getConfiguration($xmlFile) {

        if (!file_exists($xmlFile)) {
            $this->createConfigurationXMLFile($xmlFile);
        }

        $xml = simplexml_load_file($xmlFile);
        foreach ($xml->children() as $node) {
            if ($node->getName() == 'offline') {
                $this->setOffline($node);
            } else if ($node->getName() == 'offline_message') {
                $this->setOfflineMessage($node);
            } else if ($node->getName() == 'site_name') {
                $this->setSiteName($node);
            } else if ($node->getName() == 'line_per_page') {
                $this->setLinePerPage($node);
            } else if ($node->getName() == 'keyword') {
                $this->setKeyWord($node);
            } else if ($node->getName() == 'address') {
                $this->setAddress($node);
            } else if ($node->getName() == 'email') {
                $this->setEmail($node);
            } else if ($node->getName() == 'marquee') {
                $this->setMarquee($node);
            } else if ($node->getName() == 'phone_number') {
                $this->setPhoneNumber($node);
            } else if ($node->getName() == 'sales') {
                $this->setSales($node);
            }
        }
    }

    // cập nhật thông tin cấu hình
    public function updateConfiguration($xmlFile) {
        if (file_exists($xmlFile)) {
            $xml = simplexml_load_file($xmlFile);
            $xml->offline = $this->getOffline();
            $xml->offline_message = $this->getOfflineMessage();
            $xml->site_name = $this->getSiteName();
            $xml->line_per_page = $this->getLinePerPage();
            $xml->keyword = $this->getKeyWord();
            $xml->address = $this->getAddress();
            $xml->email = $this->getEmail();
            $xml->marquee = $this->getMarquee();
            $xml->phone_number = $this->getPhoneNumber();
            $xml->salse = $this->getSales();
            $xml->saveXML($xmlFile);
        }
    }

    // khởi tạo file cấu hình nếu file không tồn tại
    private function createConfigurationXMLFile($xmlFile) {

        $xmlDoc = new DOMDocument('1.0', 'UTF-8');

        $root = $xmlDoc->appendChild($xmlDoc->createElement("configuration"));

        $root->appendChild($xmlDoc->createElement("offline", 0));

        $root->appendChild($xmlDoc->createElement("offline_message", "Đang nâng cấp"));

        $root->appendChild($xmlDoc->createElement("site_name", "Công ty"));

        $root->appendChild($xmlDoc->createElement("line_per_page", 10));

        $root->appendChild($xmlDoc->createElement("keyword", ''));

        $root->appendChild($xmlDoc->createElement("address", ''));

        $root->appendChild($xmlDoc->createElement("email", ''));

        $root->appendChild($xmlDoc->createElement("marquee", ''));

        $root->appendChild($xmlDoc->createElement("phone_number", ''));
        
        $root->appendChild($xmlDoc->createElement("salse", 0));
        
        $xmlDoc->formatOutput = true;

        $xmlDoc->save($xmlFile);
    }

}

?>
